package com.bjsxt.boot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjsxt.disruptor.DisruptorTemplate;
import com.bjsxt.domain.EntrustOrder;
import com.bjsxt.mapper.EntrustOrderMapper;
import com.bjsxt.model.Order;
import org.mvel2.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class DataLoaderCmdRunner implements CommandLineRunner {

    @Autowired
    private EntrustOrderMapper entrustOrderMapper;

    @Autowired
    private DisruptorTemplate disruptorTemplate;


    @Override
    public void run(String... args) throws Exception {
     List<EntrustOrder> entrustOrders =  entrustOrderMapper.selectList(
                new LambdaQueryWrapper<EntrustOrder>().eq(EntrustOrder::getStatus,0).orderByAsc(EntrustOrder::getCreated)

        );
        if (CollectionUtils.isEmpty(entrustOrders)){
            return;
        }

        for (EntrustOrder entrustOrder:entrustOrders){
            disruptorTemplate.onData(entrustOrder2Order(entrustOrder));
        }

    }

    private Order entrustOrder2Order(EntrustOrder entrustOrder) {
        Order order = new Order();
        order.setOrderId(entrustOrder.getId().toString());
        order.setPrice(entrustOrder.getPrice());
        order.setAmount(entrustOrder.getVolume().add(entrustOrder.getDeal().negate()));
        order.setSymbol(entrustOrder.getSymbol());
        order.setOrderDirection(entrustOrder.getType().intValue());
        order.setTime(entrustOrder.getCreated().getTime());

        return order;
    }
}
